package com.cy.pj.sys.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.PageObject;
import com.cy.pj.sys.pojo.SysLog;
@Service
public class SysLogServiceImpl implements SysLogService {
	@Autowired
	private SysLogDao  sysLogDao;
	@Override
	public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent)  {
		//验证参数合法性
		//验证pageCurrent的合法性
		//不合法抛出IllegalArgumentException异常
		if(pageCurrent==null || pageCurrent<1)
			throw new IllegalArgumentException("当前页码不正确");
		//2.基于条件查询总记录
		//执行查询
		int rowCount=sysLogDao.getRowCount(username);
		//验证查询结果，假如结果不为0不再执行如下操作
		if(rowCount==0)
	          throw new ServiceException("系统没有查到对应记录");
		//3.查询当前页呈现的用户日志行为信息
		int pageSize=50;
		//
		int startIndex=(pageCurrent-1)*pageSize;
		//
		List<SysLog> records = sysLogDao.findPageObjects(username, startIndex, pageSize);
		//
		//4.封装两次查询结果并返回
		/*PageObject<SysLog> pageObject = new PageObject<>();
		
		pageObject.setPageCurrent(pageCurrent);
		pageObject.setPageSize(pageSize);
		pageObject.setRowCount(rowCount);
		pageObject.setRecords(records);
		pageObject.setPageCount((rowCount-1)/pageSize+1);
		*/
		//构造函数获取属性值
		return new PageObject<>(pageCurrent, pageSize, rowCount, records);
		//return pageObject;
	}
	@Override
	public int deleteObjects(Integer... ids) {
		//判断参数是否合法
		
		if(ids==null || ids.length==0)
			throw new IllegalArgumentException("请选择一个");
		//执行删除
		int rows;
		try {
			rows = sysLogDao.deleteObjects(ids);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new ServiceException("系统出错，正在恢复..");
		}
		//返回结果
		if(rows == 0)
			throw new ServiceException("记录可能不存在");
		
		return rows;
	}
	@Override
	public void saveObject(SysLog entity) {
		sysLogDao.insertObject(entity);
	}
		

}
